for item in collection: statement --------- --------- next statements
arr = [ 10 , 50 , 60 , 20 , 30 ] for n in arr : print(n)
1 2 3 4 5 6 7 8 9 10
arr =[ 1, 5, 6, 2, 3, 8, 7, 9] for n in arr: if n%2==0: print(n,end=' ')
6 2 8
arr =[10,50,60,20,30] s=0 for n in arr: s=s+n print("Sum is ",s)
Sum is 170
arr =[1,5,6,2,3,11,4,7,9] s=0 for n in arr: if n%2==0: s=s+n print("Sum of even nos is ",s)
Sum of even nos is 12
arr=[1,5,6,2,3,10,4] s1=0 s2=0 for n in arr: if n%2==0: s1=s1+n else: s2=s2+n print("Sum of even nos is ",s1) print("Sum of odd nos is ",s2)
Sum of even nos is 22 Sum of odd nos is 9
lst=[23,-45,12,-19,0,5,14,17,0,21,-25] c1,c2,c3=0,0,0 for val in lst : if val>0 : c1=c1+1 elif val<0 : c2=c2+1 else: c3=c3+1 print("Total Positive Numbers ",c1) print("Total Negative Numbers ",c2) print("Total Zeros ",c3)
Total Positive Numbers 6 Total Negative Numbers 3 Total Zeros 2
for item in collection : statements ---------- else: statements ----------
lst=[2,4,5,12,13,8] n=input("Enter value to search ") n=int(n) for item in lst: if n==item: print("Found . .") break else: print("Not Found . .")
Enter value to search: 13 Found.. Enter value to search: 17 Not Found.